package com.hcxy.filetransform.service;

import java.io.File;
import java.util.HashMap;

import javax.swing.JPanel;

import com.hcxy.filetransform.domain.FileTranslateInfo;
import com.hcxy.filetransform.service.FilePostService.PostFileListener;
import com.hcxy.filetransform.widget.FileLogPanel;

public class PostMessageListener implements PostFileListener {

	private static PostMessageListener _instance=null;
	private HashMap<String,JPanel> panels = new HashMap<String, JPanel>();
	private HashMap<String,FileLogPanel> views = new HashMap<String, FileLogPanel>();
	public void addPanel(String mac,JPanel panel){
		panels.put(mac, panel);
	}
	
	public void removePanel(String mac){
		panels.remove(mac);
	}
	
	public void addLogView(String token,FileLogPanel fp){
		views.put(token, fp);
	}
	
	private PostMessageListener(){}
	public static PostMessageListener getInstance(){
		if (null==_instance) {
			_instance = new PostMessageListener();
		}
		return _instance;
	}
	
	@Override
	public void onPosting(FileTranslateInfo finfo, String ip, long process) {
		FileLogPanel fp = views.get(finfo.getToken());
		if (null!=fp) {
			fp.addProcess(process);
		}
	}

	@Override
	public void postFinish(FileTranslateInfo finfo, String ip) {
		FileLogPanel fp = views.get(finfo.getToken());
		if (null!=fp) {
			fp.setFileFinish(true);
			views.remove(finfo.getToken());
		}
	}

}
